package com.fy.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class JsonUtil {
	private static final ObjectMapper MAPPER = new ObjectMapper();

	static {
		MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		MAPPER.configure(SerializationFeature.INDENT_OUTPUT, Boolean.TRUE);
	}

	public static String toJson(Object obj) {
		try {
			return MAPPER.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 将json 转换为对象
	 * 
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static <T> T parseObject(String json, Class<T> clazz) {
		return JSON.parseObject(json, clazz);
	}

	public static <T> String toJsonString(T serializableObject) {
		return JSON.toJSONString(serializableObject);
	}

	public static Map<String, Object> JsonToMap(String json) {
		Map<String, Object> map = new HashMap<String, Object>();

		if (StringUtils.isNotEmpty(json)) {
			Map<String, Object> maps = JSON.parseObject(json, new TypeReference<Map<String, Object>>() {
			});

			for (Iterator<String> it = maps.keySet().iterator(); it.hasNext();) {
				String key = it.next();
				String value = maps.get(key) + "";
				if (StringUtils.isNotEmpty(value)) {
					if (value.contains("%")) {
						String newValue = value.replaceAll("%", "\\/\\%");
						map.put(key, newValue);
					} else {
						map.put(key, value);
					}
				}
			}
		}
		if (map == null || StringUtils.isEmpty(json)) {
			return new HashMap<String, Object>();
		}
		return map;
	}

	/**
	 * 将json 转换为对象 Json2Map 去掉原JsonToMap方法中的 String newValue = value.replaceAll("%",
	 * "\\/\\%");逻辑
	 * 
	 * @param json
	 * @return
	 */
	public static Map<String, Object> Json2Map(String json) {
		Map<String, Object> map = new HashMap<String, Object>();

		if (StringUtils.isNotEmpty(json)) {
			Map<String, Object> maps = JSON.parseObject(json, new TypeReference<Map<String, Object>>() {
			});

			for (Iterator<String> it = maps.keySet().iterator(); it.hasNext();) {
				String key = it.next();
				String value = maps.get(key) + "";
				if (StringUtils.isNotEmpty(value)) {
//					if (value.contains("%")) {
//						String newValue = value.replaceAll("%", "\\/\\%");
//						map.put(key, newValue);
//					} else {
//						map.put(key, value);
//					}
					map.put(key, value);
				}
			}
		}
		if (map == null || StringUtils.isEmpty(json)) {
			return new HashMap<String, Object>();
		}
		return map;
	}

	public static <T> List<T> parseArray(String json, Class<T> clazz) {
		return JSON.parseArray(json, clazz);
	}

	/**
	 * 将JsonArrary数据转成list
	 * 
	 * @param json
	 * @return
	 */
	public static List<Map<String, Object>> JsonArraryToMap(String json) {
		List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
		if (StringUtils.isEmpty(json)) {
			return listMap;
		}

		JSONArray jsonArray = JSON.parseArray(json);
		for (int i = 0; i < jsonArray.size(); i++) {
			Map<String, Object> m = new HashMap<String, Object>();
			m = jsonArray.getJSONObject(i);
			listMap.add(m);
		}
		return listMap;
	}
	
	

	public static String parseResultByString(String string) {
		if (string == null) {
			return "";
		}
		return string.trim();
	}

}
